import random

def create_clips(data, mode):
    """
    根据剪辑模式生成剪辑片段

    :param data: 输入的数据列表
    :param mode: 剪辑模式，取值为0（不加素材）、2（少量素材）、3（中量素材）、5（大量素材）、100（全部素材）
    :return: 按照模式生成的剪辑片段列表
    """

    def chunk_text(texts, max_sentences):
        """
        将文本列表拆分为每组最大句子数的子列表
        """
        return [texts[i:i + max_sentences] for i in range(0, len(texts), max_sentences)]

    def generate_clips(data, max_sentences):
        """
        根据最大句子数生成剪辑片段
        """
        clips = []
        texts = [item['text'] for item in data]
        start_times = [item['begin_time'] for item in data]
        end_times = [item['end_time'] for item in data]

        # 计算排除的索引
        exclude_indexes = {0, len(data) - 1}
        filtered_texts = [text for i, text in enumerate(texts) if i not in exclude_indexes]

        chunks = chunk_text(filtered_texts, max_sentences)

        for chunk in chunks:
            if len(chunk) <= 1:
                continue  # 跳过包含1句或更少句子的小段

            # 获取片段中所有句子的时间范围
            start_time = start_times[texts.index(chunk[0])]
            end_time = end_times[texts.index(chunk[-1])]

            # 随机选择一个句子作为剪辑内容
            random_sentence = random.choice(chunk)

            # 获取随机选择的句子的时间范围
            random_index = texts.index(random_sentence)
            clip_begin_time = start_times[random_index]
            clip_end_time = end_times[random_index]

            clips.append({
                'text': random_sentence,
                'begin_time': clip_begin_time,
                'end_time': clip_end_time
            })

        return clips

    if mode == 0:
        # 不加素材
        return data
    elif mode == 2:
        return generate_clips(data, 2)
    elif mode == 3:
        return generate_clips(data, 3)
    elif mode == 5:
        return generate_clips(data, 5)
    elif mode == 100:
        # 生成每句文本，排除第一个和最后一个
        return [{'text': item['text'], 'begin_time': item['begin_time'], 'end_time': item['end_time']}
                for i, item in enumerate(data) if i not in {0, len(data) - 1}]
    else:
        raise ValueError("Invalid mode selected. Choose from 0, 2, 3, 5, 100.")

# 示例数据
data = [
    {"text": "揭秘国货之光", "begin_time": 190, "end_time": 1605},
    {"text": "十二载", "begin_time": 1605, "end_time": 2313},
    {"text": "匠心独运", "begin_time": 2313, "end_time": 3253},
    {"text": "这家中国品牌", "begin_time": 3256, "end_time": 4672},
    {"text": "专供看得见的", "begin_time": 4672, "end_time": 6087},
    {"text": "效果护肤", "begin_time": 6087, "end_time": 7031},
    {"text": "尤其在人参美白", "begin_time": 7031, "end_time": 8682},
    {"text": "领域口碑爆棚", "begin_time": 8682, "end_time": 10097},
    {"text": "试过的人都竖", "begin_time": 10097, "end_time": 11513},
    {"text": "大拇指", "begin_time": 11513, "end_time": 12218},
    {"text": "美白虽说是场", "begin_time": 12220, "end_time": 13636},
    {"text": "马拉松", "begin_time": 13636, "end_time": 14341},
    {"text": "但你瞧婷美小屋", "begin_time": 14343, "end_time": 15995},
    {"text": "的美白次抛精华", "begin_time": 15995, "end_time": 17646},
    {"text": "一点点见证奇迹", "begin_time": 17646, "end_time": 19297},
    {"text": "痘印溜得快", "begin_time": 19297, "end_time": 20477},
    {"text": "肤色蹭蹭亮起来", "begin_time": 20477, "end_time": 22128},
    {"text": "人家可是有美白", "begin_time": 22128, "end_time": 23779},
    {"text": "许可证的", "begin_time": 23779, "end_time": 24723},
    {"text": "实打实的数据", "begin_time": 24723, "end_time": 26138},
    {"text": "说话", "begin_time": 26138, "end_time": 26610},
    {"text": "真人测试皮肤色", "begin_time": 26610, "end_time": 28261},
    {"text": "号提升", "begin_time": 28261, "end_time": 28969},
    {"text": "14.3%", "begin_time": 28969, "end_time": 30620},
    {"text": "色素沉着含量", "begin_time": 30620, "end_time": 32036},
    {"text": "减少", "begin_time": 32036, "end_time": 32506},
    {"text": "11.65%", "begin_time": 32507, "end_time": 34395},
    {"text": "轻盈如水的质感", "begin_time": 34395, "end_time": 36046},
    {"text": "油皮混合", "begin_time": 36046, "end_time": 36990}
]

# res = music_clips = create_clips(data=data, mode=100)
# print(res)